package com.cskaoyan.controller.configuration;

import com.cskaoyan.bean.BaseRespVo;
import com.cskaoyan.bean.bo.configuration.ExpressConfUpdateBo;
import com.cskaoyan.bean.bo.configuration.MallConfUpdateBo;
import com.cskaoyan.bean.bo.configuration.OrderConfUpdateBo;
import com.cskaoyan.bean.bo.configuration.WxConfUpdateBo;
import com.cskaoyan.bean.vo.configuration.ExpressConfListVo;
import com.cskaoyan.bean.vo.configuration.MallConfListVo;
import com.cskaoyan.bean.vo.configuration.OrderConfListVo;
import com.cskaoyan.bean.vo.configuration.WxConfListVo;
import com.cskaoyan.service.configuration.ConfigService;
import com.cskaoyan.util.common.Constant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @program: market
 * @description: 显示和更新运费、商城、订单、小程序配置
 * @author: zhong
 * @create: 2022-07-16 16:39
 **/
@RestController
@RequestMapping("admin/config")
public class ConfigController {
    @Autowired
    ConfigService configService;

    /**
     * @return 商城配置信息
     * @auther zhong
     * @description admin/config/mall 显示商城配置信息
     */
    @GetMapping("mall")
    public BaseRespVo listMall() {
        MallConfListVo mallConf = configService.getMallConfOp();
        return BaseRespVo.ok(mallConf, "成功", 0);
    }

    /**
     * @param mallConf:json→六条修改信息对象
     * @return 成功或失败code
     * @auther zhong
     * @description admin/config/mall 修改商城配置信息
     */
    @PostMapping("mall")
    public BaseRespVo updateMall(@RequestBody MallConfUpdateBo mallConf) {
        int code = configService.changeMallConfOp(mallConf);
        /**
         * todo:提供鉴权信息
         * if (code == Constant.UNAUTHORIZED_CODE) {
         *             return BaseRespVo.unAuthc();
         *         }
         */
        if (code == Constant.OK_CODE) {
            return BaseRespVo.ok(null, "成功", 0);
        } else if (code == Constant.FORBIDDEN_CODE) {
            return BaseRespVo.invalidData("配置信息不能为空");
        } else return BaseRespVo.invalidData("更新失败");
    }

    /**
     * @return 运费配置
     * @auther zhong
     * @description admin/config/express 显示运费配置信息
     */
    @RequestMapping("express")
    public BaseRespVo listExpress() {
        ExpressConfListVo expressConf = configService.getExpressConfOp();
        return BaseRespVo.ok(expressConf, "成功", 0);
    }

    /**
     * @param expressConf:json→运费满减配置
     * @return 成功或失败code
     * @auther zhong
     * @description admin/config/express 修改运费配置信息
     */
    @PostMapping("express")
    public BaseRespVo updateExpress(@RequestBody ExpressConfUpdateBo expressConf) {
        int code = configService.changeExpressConfOp(expressConf);
        if (code == Constant.OK_CODE) {
            return BaseRespVo.ok(null, "成功", 0);
        } else if (code == Constant.FORBIDDEN_CODE) {
            return BaseRespVo.invalidData("配置信息不能为空");
        } else return BaseRespVo.invalidData("更新失败");
    }

    /**
     * @return 订单配置
     * @auther zhong
     * @description admin/config/order 显示订单配置信息
     */
    @RequestMapping("order")
    public BaseRespVo listOrder() {
        OrderConfListVo orderConf = configService.getOrderConfOp();
        return BaseRespVo.ok(orderConf, "成功", 0);
    }

    /**
     * @param orderConf:json→订单配置
     * @return 成功或失败code
     * @auther zhong
     * @description admin/config/order 修改订单配置信息
     */
    @PostMapping("order")
    public BaseRespVo updateOrder(@RequestBody OrderConfUpdateBo orderConf) {
        int code = configService.changeOrderConfOp(orderConf);
        if (code == Constant.OK_CODE) {
            return BaseRespVo.ok(null, "成功", 0);
        } else if (code == Constant.FORBIDDEN_CODE) {
            return BaseRespVo.invalidData("配置信息不能为空");
        } else return BaseRespVo.invalidData("更新失败");
    }

    /**
     * @return 小程序配置
     * @auther zhong
     * @description admin/config/wx 显示小程序配置信息
     */
    @RequestMapping("wx")
    public BaseRespVo listWx() {
        WxConfListVo wxConf = configService.getWxConfOp();
        return BaseRespVo.ok(wxConf, "成功", 0);
    }

    /**
     * @param wxConf:json→小程序配置
     * @return 成功或失败code
     * @auther zhong
     * @description admin/config/wx 修改小程序配置信息
     */
    @PostMapping("wx")
    public BaseRespVo updateWx(@RequestBody WxConfUpdateBo wxConf) {
        int code = configService.changeWxConfOp(wxConf);
        if (code == Constant.OK_CODE) {
            return BaseRespVo.ok(null, "成功", 0);
        }else if (code == Constant.FORBIDDEN_CODE) {
            return BaseRespVo.invalidData("配置信息不能为空");
        }  else return BaseRespVo.invalidData("更新失败");
    }


}
